'use strict'
/********************触屏效果指令*/
//按压几秒后触发
yjx.directive('touchHold', ['$rootScope', '$timeout', function touchHold($rootScope, $timeout) {
    return {
        restrict: "EAC",
        scope: false,
        link: function ($scope, $element, attr) {
            //当手指放在屏幕上触发
            $element.bind('touchstart', function (event) {
                $timeout(function () {
                    //$scope.title = '请坚持按压1.8s'
                    //点击时开始定时
                    var timer = $timeout(function () {
                        //按压触发事件
                        //alert('挑战成功！')
                        //$scope.title = ''
                       
                    }, 1800);
                    //停止按压时销毁定时器
                    $element.bind('touchend', function () {
                        //$scope.title = ''
                        $timeout.cancel(timer);
                    })
                });
            })
        }
    }
}]).directive('containerScroll', ['$rootScope', '$timeout', '$interval', function containerScroll($rootScope, $timeout, $interval) {
    return {//滑动监听容器 内部滑动被监听
        restrict: "EAC",
        scope: true,
        link: function ($scope, $element, attr) {
            var scrollcontent = $element.find('.scrollable-content');
            var scrollDownFloor = 40;//下拉超过多少刷新
            $scope.touch = {};//用于用户touch事件监听
            $scope.touch._x = 0;
            $scope.touch._y = 0;
            //滑动坐标差
            $scope.touch._xx = 0;
            $scope.touch._yy = 0;

            $rootScope._currentview = 'main'

            $scope._winheight = angular.element(window).height();
            $scope._winsroll = angular.element(window).scrollTop();
            $element.css('height', '$_' + $scope._winheight + 'rem');
            //$timeout(function(){
            //判断当前界面
            //$rootScope._currentview = (event.currentTarget.attributes.currentview).value;
            //})
            //倒计时初始化
            $scope.movetimer = null;

            var resetForbid = 0;

            function reset() {
                if (resetForbid == 0 && scrollcontent.scrollTop() == 0) {//到顶部时恢复到0
                    $timeout(function () {//归零
                        $scope.touch._x = 0;
                        $scope.touch._y = 0;
                        $scope.touch._xx = 0;
                        $scope.touch._yy = 0;
                        resetForbid++;
                        scrollcontent.css('overflow-y', 'auto');
                        //初始化之后才能下拉
                        if ($scope.touch._xx == 0 && $scope.touch._yy == 0) {
                            $scope.touch.allow2pd = true;//允许下拉
                            return;
                        } else {
                            $scope.touch.allow2pd = false;
                            return;
                        }
                    })
                } else if (resetForbid == 0) {
                    $timeout(function () {
                        $scope.touch.allow2pd = false;
                        return;
                    })
                }
            }

            reset();

            $element.bind('touchstart',
                function touchs(event) {
                    $scope.touch.end = false;
                    //判断当前界面
                    //$rootScope._currentview = (event.currentTarget.attributes.currentview).value;
                    //倒计时初始化
                    $scope.movetime = 10;
                    $scope.movetimer = $interval(function () {
                        //滑动效果触发
                        $scope.touch._mx = false;
                        $scope.touch._my = false;
                        $scope.movetime++;
                    }, 10);
                    $timeout(function () {
                        if (event.originalEvent.touches && event.originalEvent.touches.length > 0) {
                            $scope.touch._x = event.originalEvent.touches[0].clientX;
                            $scope.touch._y = event.originalEvent.touches[0].clientY;
                            $scope.touch._x0 = event.originalEvent.touches[0].clientX;
                            $scope.touch._y0 = event.originalEvent.touches[0].clientY;
                            $scope.touch._xx = 0;
                            $scope.touch._yy = 0;
                        }
                    });
                });
            $element.bind('touchmove',
                function touchs(event) {
                    $scope.touch.end = false;
                    $timeout(function () {
                        var resetNuresetForbidm = 1;
                        //触屏移动量
                        if (event.originalEvent.touches && event.originalEvent.touches.length > 0) {
                            $scope.touch.scrollTop = scrollcontent.scrollTop();
                            if ($scope.touch.scrollTop == 0 && $scope.touch._yy < scrollDownFloor) {
                                $scope.touch._x = event.originalEvent.touches[0].clientX;
                                $scope.touch._y = event.originalEvent.touches[0].clientY;
                                $scope.touch._xx = $scope.touch._x - $scope.touch._x0;
                                $scope.touch._yy = $scope.touch._y - $scope.touch._y0;
                            } else {
                                $scope.touch._x = event.originalEvent.touches[0].clientX;
                                $scope.touch._y = event.originalEvent.touches[0].clientY;
                                $scope.touch._xx = $scope.touch._x - $scope.touch._x0;
                                $scope.touch._yy = $scope.touch._y - $scope.touch._y0;
                            }
                        }
                        if ($scope.touch.scrollTop == 0) {
                            if ($scope.touch._yy > 0) {
                                scrollcontent.css('overflow-y', 'hidden');
                            }
                            if ($scope.touch._yy >= scrollDownFloor) {
                                //限制y的移动距离
                                $scope.touch._yy = scrollDownFloor;
                                //重新计算y的起始位置
                                $scope.touch._y0 = $scope.touch._y - scrollDownFloor;
                            }
                        } else {
                            resetForbid = 0;//允许重置
                            $scope.touch.allow2pd = false;//禁止下拉
                        }
                    });
                });
            $element.bind('touchend',
                function touchs(event) {
                    $scope.touch.end = true;
                    $scope.touch.end_yy = $scope.touch._yy;
                    $timeout(function () {
                        if (event.originalEvent.touches && event.originalEvent.touches.length > 0) {
                            $scope.touch._x = event.originalEvent.touches[0].clientX;
                            $scope.touch._y = event.originalEvent.touches[0].clientY;
                        }
                        //计算滑动速度
                        $scope.touch._vxx = $scope.touch._xx / $scope.movetime;
                        $scope.touch._vyy = $scope.touch._yy / $scope.movetime;
                        $interval.cancel($scope.movetimer);
                        if (Math.abs($scope.touch._vxx) > 6 || Math.abs($scope.touch._xx) > 300) {
                            $scope.touch._mx = $scope.touch._vxx;
                        } else {
                            $scope.touch._mx = false;
                        }
                        if (Math.abs($scope.touch._vyy) > 6) {
                            $scope.touch._my = $scope.touch._vyy;
                        } else {
                            $scope.touch._my = false;
                        }
                        if (scrollcontent.scrollTop() == 0) {//到顶部时恢复到0
                            $scope.touch._x = 0;
                            $scope.touch._y = 0;
                            resetForbid = 0
                        }
                    });
                    $timeout(function () {//避免下拉过快
                        reset();
                        resetForbid = 0
                    }, 500);
                });
            $(window).bind("orientationchange", function (event) {
                $timeout(function () {
                    if (event.originalEvent.touches && event.originalEvent.touches.length > 0) {
                        $scope.touch._x = event.originalEvent.touches[0].clientX;
                        $scope.touch._y = event.originalEvent.touches[0].clientY;
                        $scope.touch._winheight = angular.element(window).height();
                        $element.css('height', '$_' + $scope.touch._winheight + 'rem');
                    }
                }, 300)
            });
        }
    }
}]).directive('scrollAble',[function(){
    /*阻止微信浏览器中下拉黑底 */
    return{
        restrict:'A',
        link:function($scope,elem,attr){
            var lastY;
            elem.bind('touchstart',function(event){
                lastY = event.originalEvent.changedTouches[0].clientY;//点击屏幕时记录最后一次Y度坐标。
            });
            //elem.bind('touch')
            elem.bind('touchmove',function(event){
                var y = event.originalEvent.changedTouches[0].clientY;
                var st = elem.context.scrollTop;
                if (y >= lastY && st <= 10) {//如果滚动条高度小于0，可以理解为到顶了，且是下拉情况下，阻止touchmove事件。
                    event.preventDefault();
                }
                lastY = y;
            })
        }
    }
}]).directive('scrollDown2',[function(){
    return{
        restrict:'A',
        link:function($scope,elem,attr){
            // Handle the start of interactions
            var startY, startTopScroll;
            elem.bind('touchstart', function(event){
                //console.log(event);
                startY = event.originalEvent.touches[0].clientY;
                startTopScroll = elem.context.scrollTop;

                if(startTopScroll <= 0)
                    elem.context.scrollTop = 1;
                if(startTopScroll + elem.context.offsetHeight >= elem.context.scrollHeight)
                //alert(elem.context.scrollTop);
                    elem.context.scrollTop = elem.context.scrollHeight - elem.context.offsetHeight - 1;
            });

            elem.bind('touchmove', function(evt) {
                //if the content is actually scrollable, i.e. the content is long enough
                //that scrolling can occur
                if(elem.context.offsetHeight < elem.context.scrollHeight)
                //console.log("offsetHeight"+elem.context.offsetHeight+"scrollHeight"+elem.context.scrollHeight);
                    evt.originalEvent._isScroller = true;
            });
        }
        }
}])



